package cn.itcast.bos.web.action.base;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Join;
import javax.persistence.criteria.JoinType;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;

import org.apache.commons.lang.StringUtils;
import org.apache.struts2.ServletActionContext;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Controller;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;

import cn.itcast.bos.domain.base.Courier;
import cn.itcast.bos.service.base.CourierService;
import cn.itcast.bos.web.action.commom.BaseAction;

@ParentPackage("json-default")
@Namespace("/")
@Controller("courierAction")
@Scope("prototype")
public class CourierAction extends BaseAction<Courier> {

	@Autowired
	private CourierService courierService;

	// 保存快递员信息
	@Action(value = "courier_save", results = {
			@Result(name = "success", type = "redirect", location = "./pages/base/courier.html") })
	public String save() {
		courierService.save(model);
		return SUCCESS;
	}

	@Action(value = "courier_pageQuery", results = { @Result(name = "success", type = "json") })
	public String pageQuery() {
		// 根据查询条件构造specialfication条件查询对象
		Specification<Courier> specification = new Specification<Courier>() {
			/**
			 * 构造条件查询方法,如果方法返回null,代表无条件查询 root 用来获取条件表达式 query criteriaquery
			 * 提供where方法 cb 构造predicate 对象 条件对象 构造复杂查询效果
			 */
			@Override
			public Predicate toPredicate(Root<Courier> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
				// 当前查询root跟对象 courier
				// 单表查询 查询当前对象,对应数据表
				List<Predicate> list = new ArrayList<Predicate>();
				if (StringUtils.isNotBlank(model.getCourierNum())) {
					Predicate p1 = cb.equal(root.get("courierNum").as(String.class), model.getCourierNum());
					list.add(p1);
				}
				if (StringUtils.isNotBlank(model.getCompany())) {
					Predicate p2 = cb.like(root.get("company").as(String.class), "%" + model.getCompany() + "%");
					list.add(p2);
				}
				if (StringUtils.isNotBlank(model.getType())) {
					Predicate p3 = cb.equal(root.get("type").as(String.class), model.getType());
					list.add(p3);
				}
				// 多表查询 查询当前对象,关联对象,对应数据表
				Join<Object, Object> standardRoot = root.join("standard", JoinType.INNER);
				if (model.getStandard() != null && StringUtils.isNotBlank(model.getStandard().getName())) {
					// 进行收派标准
					Predicate p4 = cb.like(standardRoot.get("name").as(String.class),
							"%" + model.getStandard().getName() + "%");
					list.add(p4);
				}
				return cb.and(list.toArray(new Predicate[0]));
			}
		};
		Pageable pageable = new PageRequest(page - 1, rows);
		Page<Courier> pageData = courierService.findPageData(specification, pageable);
		pushPageDataToValueStack(pageData);
		return SUCCESS;
	}

	private String ids;

	public void setIds(String ids) {
		this.ids = ids;
	}

	// 批量删除快递员
	@Action(value = "courier_delBatch", results = {
			@Result(name = "success", type = "redirect", location = "./pages/base/courier.html") })
	public String delBatch() {
		String[] idArray = ids.split(",");
		courierService.delBatch(idArray);
		return SUCCESS;
	}
	// 批量还原快递员

	@Action(value = "courier_returnBatch", results = {
			@Result(name = "success", type = "redirect", location = "./pages/base/courier.html") })
	public String returnBatch() {
		String[] idArray = ids.split(",");
		courierService.returnBatch(idArray);
		return SUCCESS;
	}
	@Action(value="courier_findNoAssociation",results={@Result(name="success",type="json")})
	public String findNoAssociation(){
		List<Courier> couriers = courierService.findNoAssociation();
		ActionContext.getContext().getValueStack().push(couriers);
		return SUCCESS;
	}

}
